from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView
from .ser import *
class DianpuAPIView(APIView):
    def get(self,request):
        data = Dianpu.objects.all()
        ser = DianpuSer(data,many=True)
        return Response(ser.data)
    def post(self,request):
        data = request.data
        ser = DianpuSer(data=data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response(ser.data)
class ShangpinAPIView(APIView):
    def get(self,request):
        cate_id = request.GET.get('cate_id')
        if cate_id:
            page = Shangpin.objects.filter(cate_id=cate_id)
        else:
            page = Shangpin.objects.all()
        ser = ShangpinSer(page,many=True)
        return Response(ser.data)
    def post(self,request):
        data = request.data
        ser = ShangpinSer(data=data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response(ser.data)
class Shancha(APIView):
    def delete(self,request,pk):

        Shangpin.objects.filter(id=pk).delete()
        return Response(status=204)
    def get(self,request,pk):
        try:
            aa = Shangpin.objects.get(id=pk)
        except Exception:
            return Response(status=404)
        ser = ShangpinSer(aa)
        return Response(ser.data)

